<!-- 游戏工程母包弹窗 -->
<template>
  <el-dialog
    :title="'工程母包列表'"
    :visible="visible"
    width="1200px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" label-width="150px">
      <el-row :gutter="15">
        <el-col :lg="2" :md="4">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            style="margin-bottom: 5px;"
            @click="openEngineeringEdit()"
            >添加
          </el-button>
        </el-col>
      </el-row>
    </el-form>
    <ele-pro-table
      ref="table"
      :where="where"
      :datasource="url"
      :columns="columns"
      :selection.sync="selection"
      height="calc(100vh - 500px)"
    >
      <template slot="action" slot-scope="{ row }">
        <el-link
          type="primary"
          :underline="false"
          icon="el-icon-edit"
          @click="openEngineeringEdit(row)"
          >编辑
        </el-link>
      </template>
    </ele-pro-table>
    <!-- 编辑弹窗 -->
    <gameEngineeringEdit
      :data="current"
      :visible.sync="showEngineeringEdit"
      :gameEditID.sync="gameEditID"
      :gameID.sync="gameID"
      @done="reload"
    />
  </el-dialog>
</template>

<script>
import { mapGetters } from "vuex";
import gameEngineeringEdit from "./game-engineeringEdit";

export default {
  name: "GameEngineeringPackage",
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object,
    gameID: {
      type: [Number, String]
    }
  },
  components: { gameEngineeringEdit },
  computed: {
    ...mapGetters(["permission"])
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({ status: 1 }, this.data),
      where: {},
      // 表单验证规则
      // rules: {
      //   bundle_id: : [
      //     { required: true, message: "请输入关联项目", trigger: "blur" }
      //   ],
      //   channel_id: [
      //     { required: true, message: "请输入推广渠道", trigger: "blur" }
      //   ],
      //   game_name: [
      //     { required: true, message: "请输入游戏名称", trigger: "blur" }
      //   ],
      //   icon: [{ required: true, message: "请输入icon", trigger: "blur" }],
      //   sdk_version: [
      //     {
      //       required: true,
      //       message: "请输入区别版本",
      //       trigger: "blur"
      //     }
      //   ],
      //   game_appid: [
      //     { required: true, message: "请输入游戏appid", trigger: "blur" }
      //   ],
      //   relation_game_id: [
      //     { required: true, message: "请输入", trigger: "blur" }
      //   ],
      //   relation_game_name: [
      //     { required: true, message: "请输入游戏名称", trigger: "blur" }
      //   ],
      //   pay_notify_url: [
      //     { required: true, message: "请输入游戏支付通知地址", trigger: "blur" }
      //   ],
      //   access_key: [
      //     { required: true, message: "请输入访问秘钥", trigger: "blur" }
      //   ],
      //   status: [
      //     {
      //       required: true,
      //       message: "请输入状态",
      //       trigger: "blur"
      //     }
      //   ]
      // },
      // 是否显示编辑弹窗
      showEngineeringEdit: false,
      url: "/Gameorigin/index",
      selection: [],
      current: null,
      gameEditID: null,
      // 表格列配置
      columns: [
        {
          columnKey: "selection",
          type: "selection",
          width: 45,
          align: "center",
          fixed: "left"
        },
        {
          prop: "id",
          label: "id",
          width: 150,
          align: "center",
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: "bundle_id",
          label: "包名",
          width: 200,
          align: "center",
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: "path",
          label: "母包地址",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center"
        },
        {
          prop: "version_code",
          label: "版本号",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center"
        },
        {
          columnKey: "action",
          label: "操作",
          width: 150,
          align: "center",
          resizable: false,
          slot: "action",
          fixed: "right"
        }
      ],
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    // gameID() {
    //   console.log(this, "ttttthis");
    //   console.log(this.reload, "ttttthisLOAD");
    //   this.reload();
    // },
    "form.channel_id"(val) {
      if (val === 0) {
        this.$delete(this.form, "pay_notify_url");
      } else {
        this.$set(this.form, "pay_notify_url", "");
      }
    },
    visible(val) {
      if (val) {
        this.$set(this.where, "game_id", this.gameID);
        this.reload();
        if (this.data) {
          this.form = Object.assign({}, this.data);
          this.isUpdate = true;
        } else {
          this.form = {};
          this.isUpdate = false;
        }
      }
    }
  },
  methods: {
    /* 保存编辑 */
    // changeAppid(val) {
    //   this.$set(this.form, 'app_id', val)
    //   console.log(this.form)
    // },
    /* 刷新表格 */
    reload() {
      this.$refs.table ? this.$refs.table.reload({ where: this.where }) : "";
    },
    save() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true;
          this.$http
            .post("/game/edit", this.form)
            .then(res => {
              this.loading = false;
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                if (!this.isUpdate) {
                  this.form = {};
                }
                this.updateVisible(false);
                this.$emit("done");
                this.reload();
              } else {
                this.$message.error(res.data.msg);
                this.reload();
              }
            })
            .catch(e => {
              this.loading = false;
              this.$message.error(e.message);
              this.reload();
            });
        } else {
          return false;
        }
      });
    },
    /* 显示编辑 */
    openEngineeringEdit(row) {
      this.current = row;
      this.gameEditID = row ? row.id : null;
      this.showEngineeringEdit = true;
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit("update:visible", value);
    }
  }
};
</script>

<style scoped></style>
